/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ins.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ins.entity.InsDashboard;
import com.thinkgem.jeesite.modules.ins.entity.InsField;
import com.thinkgem.jeesite.modules.ins.entity.InsQuota;
import com.thinkgem.jeesite.modules.ins.entity.InsTable;
import com.thinkgem.jeesite.modules.ins.service.InsFieldService;
import com.thinkgem.jeesite.modules.ins.service.InsQuotaService;
import com.thinkgem.jeesite.modules.ins.service.InsTableService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 指标卡规则Controller
 * @author fanqinglong
 * @version 2018-03-15
 */
@Controller
@RequestMapping(value = "${adminPath}/ins/insQuota")
public class InsQuotaController extends BaseController {

	@Autowired
	private InsQuotaService insQuotaService;
	@Autowired
	private InsTableService insTableService;
	@Autowired
	private InsFieldService insFieldService;
	
	@ModelAttribute
	public InsQuota get(@RequestParam(required=false) String id) {
		InsQuota entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = insQuotaService.get(id);
		}
		if (entity == null){
			entity = new InsQuota();
		}
		return entity;
	}
	
	@RequiresPermissions("ins:insQuota:view")
	@RequestMapping(value = {"quotaIndex", ""})
	public String quotaIndex(){
		return "modules/ins/insQuotaIndex";
	}
	
	@RequiresPermissions("ins:insQuota:view")
	@RequestMapping(value = {"list"})
	public String list(InsQuota insQuota, HttpServletRequest request, HttpServletResponse response, Model model) {
		insQuota.setYaxisType("7");
		//所有指标卡
		List<InsQuota> list = insQuotaService.findList(insQuota);
		//非批标卡
		List<InsQuota> notQuotaList = insQuotaService.findNotQuotaList(insQuota);
		model.addAttribute("insQuota", insQuota);
		model.addAttribute("list", list);
		model.addAttribute("notQuotaList", notQuotaList);
		return "modules/ins/insQuotaList";
	}

	@RequiresPermissions("ins:insQuota:view")
	@RequestMapping(value = "form")
	public String form(InsQuota insQuota, Model model) {
		model.addAttribute("insQuota", insQuota);
		InsTable insTable = insTableService.get(insQuota.getTable());
		model.addAttribute("table", insTable);
		if(!StringUtils.isEmpty(insTable.getId())){
			InsField insField = new InsField();
			insField.setTable(insTable);
			insField.setType("2");
			List<InsField> numberFields = insFieldService.findList(insField);
			List<InsField> notNumberFields = insFieldService.findNotNumberList(insField);
			model.addAttribute("numberFields", numberFields);
			model.addAttribute("notNumberFields", notNumberFields);
		}
		return "modules/ins/insQuotaForm";
	}
	
	@RequiresPermissions("ins:insQuota:view")
	@RequestMapping(value = "newForm")
	public String newForm(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		String tableId = request.getParameter("table.id");
		String dashboardId = request.getParameter("dashboardId");
		InsQuota insQuota = new InsQuota(id);
		insQuota.setTable(new InsTable(tableId));
		insQuota.setDashboard(new InsDashboard(dashboardId));
		model.addAttribute("insQuota", insQuota);
		InsTable insTable = insTableService.get(insQuota.getTable());
		model.addAttribute("table", insTable);
		if(!StringUtils.isEmpty(insTable.getId())){
			InsField insField = new InsField();
			insField.setTable(insTable);
			insField.setType("2");
			List<InsField> numberFields = insFieldService.findList(insField);
			List<InsField> notNumberFields = insFieldService.findNotNumberList(insField);
			model.addAttribute("numberFields", numberFields);
			model.addAttribute("notNumberFields", notNumberFields);
		}
		//清空所有配置，包括无视已存在 数据库里的Y轴配置
		model.addAttribute("newForm", true);
		return "modules/ins/insQuotaForm";
	}

	/**
	 * 选择工作表
	 * @param model
	 * @return
	 */
	@RequiresPermissions("ins:insQuota:edit")
	@RequestMapping(value = "selectTable")
	public String selectTable(HttpServletRequest request,Model model){
		String id = request.getParameter("id");
		String dashboardId = request.getParameter("dashboardId");
		InsTable insTable = new InsTable();
		//如果当前用户不具有查看所有数据表的权限，则查询出他能够查看的数据表
		if (!SecurityUtils.getSubject().isPermitted("ins:insTable:viewAll")) {
			insTable.getSqlMap().put("dsf", InsTableService.getDSF(UserUtils.getUser().getId(), ""));
		}
		List<InsTable> tables = insTableService.findList(insTable);
		InsQuota insQuota = new InsQuota(id);
		insQuota.setDashboard(new InsDashboard(dashboardId));
		model.addAttribute("tables", tables);
		model.addAttribute("insQuota", insQuota);
		return "modules/ins/insSelectTable";
	}
	
	@RequiresPermissions("ins:insQuota:edit")
	@RequestMapping(value = "save")
	public String save(InsQuota insQuota, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, insQuota)){
			return form(insQuota, model);
		}
		//当前登录的人员
		User user = UserUtils.getUser();
		insQuota.setUser(user);
		insQuotaService.save(insQuota);
		addMessage(redirectAttributes, "保存图表成功");
		return "redirect:"+Global.getAdminPath()+"/ins/insQuota/list?repage&dashboard.id="+insQuota.getDashboard().getId();
	}
	
	@RequiresPermissions("ins:insQuota:edit")
	@RequestMapping(value = "delete")
	public String delete(InsQuota insQuota, RedirectAttributes redirectAttributes) {
		insQuotaService.delete(insQuota);
		addMessage(redirectAttributes, "删除图表成功");
		return "redirect:"+Global.getAdminPath()+"/ins/insQuota/list?repage&dashboard.id="+insQuota.getDashboard().getId();
	}

	@RequiresPermissions("ins:insQuota:edit")
	@RequestMapping(value = "getFields")
	@ResponseBody
	public List<InsField> getFields(InsQuota insQuota){
		InsField insField = new InsField();
		insField.setTable(insQuota.getTable());
		insField.setType("2");
		List<InsField> fields = insFieldService.findList(insField);
		return fields;
	}
	
	@RequiresPermissions("ins:insQuota:view")
	@RequestMapping(value = "getResult")
	@ResponseBody
	public double getResult(InsQuota insQuota){
		return insQuotaService.getResult(insQuota);
	}
	
	@RequiresPermissions("ins:insQuota:view")
	@RequestMapping(value = "getData")
	@ResponseBody
	public String getData(InsQuota insQuota){
		JSONObject result = insQuotaService.getData(insQuota);
		return result.toString();
	}
	@RequiresPermissions("ins:insQuota:edit")
	@RequestMapping(value = "updateEnableSettingValue")
	@ResponseBody
	public String updateEnableSettingValue(HttpServletRequest request){
		String id = request.getParameter("id");
		InsQuota insQuota = new InsQuota(id);
		insQuota.setEnableSettingValue("");
		insQuotaService.updateEnableSettingValue(insQuota);
		return "success";
	}
}